<?php

/**
 * This is the model class for table "tbl_service".
 *
 * The followings are the available columns in table 'tbl_service':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property string $create_time
 * @property string $update_time
 * @property string $release_date
 * @property string $expiry_date
 * @property integer $create_user_id
 * @property integer $update_user_id
 * @property string $price
 *
 * The followings are the available model relations:
 * @property User $createUser
 * @property User $updateUser
 */
class Service extends SeoVietActiveRecord {

    //auto set create_user_id and update_user_id
    protected function beforeValidate() {
        if ($this->isNewRecord) {
            $this->create_user_id = $this->update_user_id = Yii::app()->user->id;
        } else {
            $this->update_user_id = Yii::app()->user->id;
        }

        return parent::beforeValidate();
    }

    public function getCreateUserText(){
        return (isset($this->create_user_id)) ?
                $this->createUser->username :
                "unknown user id ({$this->create_user_id})";
    }
    
    public function getUpdateUserText(){
    	return (isset($this->update_user_id)) ?
    			$this->updateUser->username :
    			"unknown user id ({$this->update_user_id})";
    }

    /**
     * Returns the static model of the specified AR class.
     * @return Service the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_service';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, description, release_date, expiry_date, price', 'required'),
            
            //validate date
            array('release_date, expiry_date', 'date', 'format'=>'yyyy-M-d'),

            array('create_user_id, update_user_id', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 128),
            array('price', 'length', 'max' => 20),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id,name, description,  release_date, expiry_date,  price', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'createUser' => array(self::BELONGS_TO, 'User', 'create_user_id'),
            'updateUser' => array(self::BELONGS_TO, 'User', 'update_user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'description' => 'Description',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'release_date' => 'Release Date',
            'expiry_date' => 'Expiry Date',
            'create_user_id' => 'Create User',
            'update_user_id' => 'Update User',
            'price' => 'Price',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('create_time', $this->create_time, true);
        $criteria->compare('update_time', $this->update_time, true);
        $criteria->compare('release_date', $this->release_date, true);
        $criteria->compare('expiry_date', $this->expiry_date, true);
        $criteria->compare('create_user_id', $this->create_user_id);
        $criteria->compare('update_user_id', $this->update_user_id);
        $criteria->compare('price', $this->price, true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}